/*
 * @Author: szx
 * @Date: 2022-02-26 12:54:40
 * @LastEditTime: 2022-02-26 12:56:57
 * @Description:
 * @FilePath: \leetcode\1000-1099\1046\1046.cpp
 */

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class Solution {
   public:
    int lastStoneWeight(vector<int>& stones) {
        // 优先队列，也就是最大堆
        priority_queue<int> q;
        for (int s : stones) {
            q.push(s);
        }

        while (q.size() > 1) {
            int a = q.top();
            q.pop();
            int b = q.top();
            q.pop();
            if (a > b) {
                q.push(a - b);
            }
        }
        return q.empty() ? 0 : q.top();
    }
};
